<template>
  <div class="cart">
    <TabNav v-model="isShowTabNav"></TabNav>
    <h4>全部商品</h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <div class="cart-body">
        <ul class="cart-list" v-for="(good,index) in shopCartList" :key="good.id">
          <li class="cart-list-con1">
            <input type="checkbox" name="chk_list" :checked="good.isChecked" @change="updateChecked(good.skuId,$event.target.checked)">
          </li>
          <li class="cart-list-con2">
            <img :src="good.imgUrl">
            <div class="item-msg">{{ good.skuName }}</div>
          </li>
          <li class="cart-list-con4">
            <span class="price">{{ good.skuPrice }}</span>
          </li>
          <li class="cart-list-con5">
            <button class="mins" @click="changeNum('sub',good,-1)" :disabled="good.skuNum<=1">-</button>
            <input type="text" class="itxt" :value="good.skuNum" @change="changeNum('change',good,$event.target.value*1)">
            <button class="plus" @click="changeNum('add',good,1)">+</button>
          </li>
          <li class="cart-list-con6">
            <span class="sum">{{ good.skuPrice * good.skuNum }}</span>
          </li>
          <li class="cart-list-con7">
            <a class="sindelet" @click="deleteGood(good.skuId)">删除</a>
          </li>
        </ul>
      </div>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" :checked="isAllChecked&&shopCartListLength" @change="updateAllChecked($event.target.checked)"> <span>全选</span>
      </div>
      <div class="option">
        <a @click="deleteCheckedGood">删除选中的商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">已选择<span>{{ shopCartListLength }}</span>件商品</div>
        <div class="sumprice">
          <em>总价(不含运费):</em> <i class="summoney">{{ totalPrice.toFixed(2) }}</i>
        </div>
        <div class="sumbtn">
          <a class="sum-btn" @click="$router.push('/trade')">结算</a>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {mapActions, mapGetters, mapState} from "vuex";
import {debounce, throttle} from "lodash"

export default {
  name: 'ShopCart',
  data() {
    return {
      isShowTabNav: false
    }
  },
  computed: {
    /* 使用mapState将shopCartStore模块下的state映射为组件实例的属性 */
    ...mapState("shopCartStore", ["shopCartList"]),
    ...mapGetters("shopCartStore", ["shopCartListLength", "totalPrice", "isAllChecked"])
  },
  methods: {
    /* 使用mapActions将shopCartStore模块下的actions映射为组件实例的方法 */
    ...mapActions("shopCartStore", ["fetchShopCartList", "deleteCartGood", "changeCartGood", "deleteCartCheckedGood", "changeCartAllGood"]),
    ...mapActions("detailStore", ["addOrUpdateShopCart"]),
    /* 点击删除按钮的回调函数(删除某一个商品) */
    async deleteGood(id) {
      const confirm = await this.$confirm('确定删除此商品吗?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning',lockScroll:false}).catch(e => e)
      if (confirm === "cancel") return
      try {
        const result = await this.deleteCartGood(id)
        await this.fetchShopCartList()
        this.$message.success(result)
      } catch (e) {
        this.$message.error(e)
      }
    },
    /* 点击单选按钮的回调函数(修改某一个商品) */
    async updateChecked(skuId, checked) {
      try {
        const isChecked = checked ? 1 : 0
        const result = await this.changeCartGood({skuId, isChecked})
        await this.fetchShopCartList()
        this.$message.success(result)
      } catch (e) {
        this.$message.error(e)
      }
    },
    /* 点击删除选中的商品按钮的回调函数(删除选中的商品) */
    async deleteCheckedGood() {
      const confirm = await this.$confirm('确定删除选中的商品吗?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).catch(e => e)
      if (confirm === "cancel") return
      try {
        const result = await this.deleteCartCheckedGood()
        this.$message.success(result[0])
        this.fetchShopCartList()
      } catch (error) {
        this.$message.error(error)
      }
    },
    /* 点击选项按钮的回调函数(修改全部商品) */
    async updateAllChecked(checked) {
      try {
        const isChecked = checked ? 1 : 0
        const result = await this.changeCartAllGood(isChecked)
        this.$message.success(result[0])
        this.fetchShopCartList()
      } catch (error) {
        this.$message.error(error)
      }

    },
    /*  修改购物车商品数量的回调函数 */
    changeNum: debounce(async function (type, good, skuNum) {
      switch (type) {
        case "add":
          skuNum = 1
          break
        case "sub":
          skuNum = good.skuNum > 1 ? -1 : 0
          break
        default:
          skuNum = (isNaN(skuNum) || skuNum < 1) ? 0 : Math.ceil(skuNum - good.skuNum)
          /*if (isNaN(skuNum) || skuNum < 1) {
            skuNum = 0
          } else {
            skuNum = Math.ceil(skuNum - good.skuNum)
          }*/
      }
      try {
        await this.addOrUpdateShopCart({skuId: good.skuId, skuNum})
        await this.fetchShopCartList()
      } catch (e) {
        console.log(e.message)
      }
    }, 1000)
  },
  mounted() {
    /* 组件挂载完毕的时候调用fetchShopCartList()派发actions获取购物车的数据 */
    this.fetchShopCartList()
  }
}
</script>
<style lang="less" scoped>
.cart {
  width: 1200px;
  margin: 0 auto;

  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }

  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;

      & > div {
        float: left;
      }

      .cart-th1 {
        width: 25%;

        input {
          vertical-align: middle;
        }

        span {
          vertical-align: middle;
        }
      }

      .cart-th2 {
        width: 25%;
      }

      .cart-th3,
      .cart-th4,
      .cart-th5,
      .cart-th6 {
        width: 12.5%;

      }
    }

    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;

      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;
        height: 103px;
        line-height: 103px;

        & > li {
          float: left;
        }

        .cart-list-con1 {
          width: 15%;

          input {
            cursor: pointer;
          }
        }

        .cart-list-con2 {
          display: flex;
          align-items: center;
          width: 35%;

          img {
            width: 82px;
            height: 82px;
            float: left;
          }

          .item-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }


        .cart-list-con4 {
          width: 10%;

        }

        .cart-list-con5 {
          width: 17%;
          display: flex;
          align-items: center;
          height: 103px;

          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
            cursor: pointer;
            height: 35px;
          }

          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 33px;
            float: left;
            text-align: center;
            font-size: 14px;
            outline: none;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
            cursor: pointer;
            height: 35px;

          }
        }

        .cart-list-con6 {
          width: 10%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 13%;

          a {
            color: #666;

            &:hover {
              color: #c00000;
            }
          }
        }
      }
    }
  }

  .cart-tool {
    display: flex;
    justify-content: space-between;
    align-items: center;
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        cursor: pointer;
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;

        &:hover {
          color: #c00000;
        }
      }
    }

    .money-box {
      display: flex;
      align-items: center;
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
        font-size: 12px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 12px;
          cursor: pointer;
        }
      }

      .sumbtn {
        float: right;

        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: "Microsoft YaHei";
          background: #e1251b;
          overflow: hidden;
        }
      }
    }
  }
}
</style>
